﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour {

    public float moveSpeed;
    public float jumpForce;
    public float hurtForce;
    public float hurtTime;
    public float crouchMutiple;
    public float climbSpeed;
    public float dashSpeed;
    public float dashTime;
    public float dashCoolDown;
    public Transform footPos;
    public Transform crouchPos;
    public LayerMask groundMask;
    public LayerMask enemyMask;

    private Rigidbody2D rb;
    private Animator anim;
    private float horizontal;
    private float vertical;
    private bool isGround;
    private int extraJump;
    private bool isCrouch;
    private bool isClimb;
    private bool isLadder;
    private bool isHurt;
    private bool isDash;

    private float hurtTimer;
    private float dashTimer;
    private float dashCoolTimer;

	// Use this for initialization
	void Start () {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {
        ProcessInput();
        Animate();

    }

    void ProcessInput()
    {
        //受伤
        if (isHurt)
        {
            hurtTimer -= Time.deltaTime;
            if (hurtTimer <= 0)
            {
                isHurt = false;
                rb.velocity = Vector2.zero;
            }
            return;
        }

        //冲锋
        if (Input.GetKeyDown(KeyCode.Z) && dashCoolTimer <= 0)
        {
            isDash = true;
            dashTimer = dashTime;
            dashCoolTimer = dashCoolDown;
        }
        if (dashCoolTimer > 0)
        {
            dashCoolTimer -= Time.deltaTime;
        }
        if (isDash)
        {
            dashTimer -= Time.deltaTime;
            //ShadowPool.Instance.GetFromPool();
            ObjectPool.Instance.GetFromPool("shadow");
            rb.velocity = dashSpeed * Vector2.right*(GetComponent<SpriteRenderer>().flipX==true?-1:1);
            if (dashTimer <= 0)
            {
                isDash = false;
                rb.velocity = Vector2.zero;
            }
            return;
        }

        //移动
        horizontal = Input.GetAxis("Horizontal");
        vertical = rb.velocity.y;
        Move();
        isGround = Physics2D.OverlapCircle(footPos.position, 0.1f, groundMask);

        //下蹲
        if (!Physics2D.OverlapCircle(crouchPos.position, 0.3f, groundMask))
        {
            if (Input.GetKey(KeyCode.DownArrow))
            {
                GetComponent<BoxCollider2D>().enabled = false;
                isCrouch = true;
                //moveSpeed *= 0.7f;
            }
            else
            {
                isCrouch = false;
                GetComponent<BoxCollider2D>().enabled = true;
                //moveSpeed /= 0.7f;
            }
        }
        if (isCrouch)
            crouchMutiple = 0.7f;
        else
            crouchMutiple = 1f;

        //跳跃
        if (isGround)
            extraJump = 2;
        if (Input.GetKeyDown(KeyCode.C) && extraJump>0)
        {
            isGround = false;
            extraJump--;
            rb.velocity = Vector2.up * jumpForce;
        }

        //爬楼梯
        if (isLadder)
        {
            float dir = Input.GetAxis("Vertical");
            if (dir != 0)
            {
                rb.velocity = Vector2.up * climbSpeed * dir;
                rb.gravityScale = 0;
                isClimb = true;
            }
            else
                rb.velocity = new Vector2(rb.velocity.x, 0);
        }
        if (isLadder == false)
        {
            rb.gravityScale = 1;
            isClimb = false;
        }
    }

    void Animate()
    {
        anim.SetFloat("Horizontal", Mathf.Abs(horizontal));
        anim.SetFloat("Vertical", vertical);
        anim.SetBool("IsGround", isGround);
        anim.SetBool("IsHurt", isHurt);
        anim.SetBool("IsCrouch", isCrouch);
        anim.SetBool("IsClimb", isClimb);
;
    }

    void Move()
    {
        if (horizontal != 0)
        {
            rb.velocity = new Vector2(horizontal * moveSpeed * crouchMutiple, rb.velocity.y);
            if (horizontal < 0)
            {
                GetComponent<SpriteRenderer>().flipX = true;
            }
            else
            {
                GetComponent<SpriteRenderer>().flipX = false;
            }
           
        }
        
        
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (isHurt)
            return;
        if (other.gameObject.tag == "Enemy")
        {
            if (Physics2D.OverlapCircle(footPos.position, 0.3f, enemyMask))
            {
                Enemy e = other.gameObject.GetComponent<Enemy>();
                e.OnDie();
                rb.velocity = Vector2.up * jumpForce;
            }
            else
            {
                isHurt = true;
                hurtTimer = hurtTime;
                Vector2 dir = -(other.gameObject.transform.position - transform.position).normalized;
                rb.velocity=dir*hurtForce;
            }
        }
        
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.gameObject.tag == "Ladder")
        {
            isLadder = true;
        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.tag == "Ladder")
        {
            isLadder = false;
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(crouchPos.position, 0.3f);
    }

}
